//
//  GatherAuthorization.m
//  LiveGather
//
//  Created by macintosh on 16/9/15.
//  Copyright © 2016年 Yue Shen. All rights reserved.
//

#import "GatherAuthorization.h"
#import <AVFoundation/AVFoundation.h>


@implementation GatherAuthorization

+ (BOOL)verifyCameraAuthorization:(UIViewController *)vc
{
    BOOL hasAuthorization = NO;
    AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];//相机权限
    switch (AVstatus) {
        case AVAuthorizationStatusAuthorized:
            hasAuthorization = YES;
            break;
        case AVAuthorizationStatusDenied:
        case AVAuthorizationStatusNotDetermined:
        {
            NSString *message = [NSString stringWithFormat:@"请在iPhone的“设置-隐私-相机”选项中，允许%@访问你的手机相机", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"]];
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"无法使用相机" message:message preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alert addAction:action];
            [vc presentViewController:alert animated:YES completion:nil];
        }
            break;
        case AVAuthorizationStatusRestricted:
        {
            NSString *message = [NSString stringWithFormat:@"设备不支持相机功能"];
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"无法使用相机" message:message preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alert addAction:action];
            [vc presentViewController:alert animated:YES completion:nil];
        }
            break;
        default:
            break;
    }
    return hasAuthorization;
}

+ (BOOL)verifyMicrophoneAuthorization:(UIViewController *)vc
{
    BOOL hasAuthorization = NO;
    AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];//麦克风权限
    switch (AVstatus) {
        case AVAuthorizationStatusAuthorized:
            hasAuthorization = YES;
            break;
        case AVAuthorizationStatusDenied:
        case AVAuthorizationStatusNotDetermined:
        {
            NSString *message = [NSString stringWithFormat:@"请在iPhone的“设置-隐私-麦克风”选项中，允许%@访问你的手机麦克风", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"]];
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"无法使用麦克风" message:message preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alert addAction:action];
            [vc presentViewController:alert animated:YES completion:nil];
        }
            break;
        case AVAuthorizationStatusRestricted:
        {
            NSString *message = [NSString stringWithFormat:@"设备不支持麦克风功能"];
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"无法使用麦克风" message:message preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alert addAction:action];
            [vc presentViewController:alert animated:YES completion:nil];
        }
            break;
        default:
            break;
    }
    return hasAuthorization;
}

@end
